<template>
	<view class="wrap">
		<view class="topBc"></view>
		<view class="content formWrap">
			<view class="title" style="margin-top: 0;">党建信息</view>
			
			<u-form :model="formData" ref="uForm" :rules="rules">
				<u-form-item v-if="formKeys.partybuilding" label="是否有党建信息" prop="partybuilding" required label-width="auto" borderBottom>
					<u-radio-group v-model="formData.partybuilding" placement="row">
						<u-radio v-for="item,index in stateRanges" :key="index" :label="item.text" :name="item.value" :activeColor="bgColor"></u-radio>
					</u-radio-group>
				</u-form-item>
				<u-form-item v-if="formKeys.parins" label="党组织建制" prop="parins" required label-width="auto" borderBottom @click="showSelect=true">
					<u--input v-model="formData.parinsCn" placeholder="请选择" border="none" inputAlign="right" disabled disabledColor="rgba(0,0,0,0)"></u--input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>
				<u-form-item v-if="formKeys.numparm" label="中共党员（包括预备党员）人数" prop="numparm" required label-width="auto" borderBottom>
					<u-input v-model="formData.numparm" placeholder="请输入" border="none" inputAlign="right">
						<u--text
							text="人"
							slot="suffix"
						></u--text>
					</u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.resparmsign" label="经营者是否为党员" prop="resparmsign" required label-width="auto" borderBottom>
					<u-radio-group v-model="formData.resparmsign" placement="row">
						<u-radio v-for="item,index in stateRanges" :key="index" :label="item.text" :name="item.value" :activeColor="bgColor"></u-radio>
					</u-radio-group>
				</u-form-item>
				<u-form-item v-if="formKeys.resparsecsign" label="经营者是否为党组织书记" prop="resparsecsign" required label-width="auto" borderBottom>
					<u-radio-group v-model="formData.resparsecsign" placement="row">
						<u-radio v-for="item,index in stateRanges" :key="index" :label="item.text" :name="item.value" :activeColor="bgColor"></u-radio>
					</u-radio-group>
				</u-form-item>
			</u-form>
			
		</view>
		<view class="submitBtn_bc"></view>
		<view class="submitBtn_ubtn">
			<u-button :disabled="!$util.submitState(year,state)" @click="submit()">提交</u-button>
		</view>
		
		<u-action-sheet :show="showSelect" :actions="typeList" cancelText="取消" @close="showSelect = false" @select="selectBtn"></u-action-sheet>
	</view>
</template>

<script>
export default {
	data(){
		return{
			minFormName: 'partybuilding', //小表单类型名
			regnoInfo: this.$store.state.regnoInfo,
			year: '',
			state: '',
			bgColor: this.$config.bgColor,
			
			showSelect: false,
			stateRanges: [
				{text: '是',value: '1'},
				{text: '否',value: '2'}
			],
			typeList: [],
			formData: {
				partybuilding: '2',
				parins: '4',
				parinsCn: '',
				numparm: '0',
				resparmsign: '2',
				resparsecsign: '2',
			},
			rules: {},
			rulesAll: {
				partybuilding: this.$typeData.ruleData.disRule,
				parins: this.$typeData.ruleData.disRule,
				numparm: this.$typeData.ruleData.peopleRule,
				resparmsign: this.$typeData.ruleData.disRule,
				resparsecsign: this.$typeData.ruleData.disRule,
			},
			formKeys: {},
			
		}
	},
	onLoad(option) {
		this.year = option.year;
		this.state = option.state;
		this.typeList = this.$util.getLocalDictMap('parins','id','name');
		this.getFormKey();
		this.getFormData();
	},
	onReady() {
		this.$refs.uForm.setRules(this.rules)
	},
	methods:{
		// 获取小表单数据
		getFormData(){
			let url = this.$typeData.minFormUrl.get(this.minFormName);
			if(!url){
				uni.showModal({
					title: '暂无接口'
				})
				return;
			}
			this.$http.sendRequest({
				url: url,
				data: {
					regno: this.regnoInfo.regno,
					ancheyear: this.year
				}
			})
			.then(res=>{
				console.log('接口返回',res.data);
				let data = res.data;				let keys = Object.keys(this.formKeys);				keys.forEach(item=>{					this.formData[item] = data[item];				})
				
				// 特殊处理，若没有数据设置默认
				this.setInit('formData','partybuilding','2');
				this.setInit('formData','numparm','0');
				this.setInit('formData','parins','4');
				this.setInit('formData','resparmsign','2');
				this.setInit('formData','resparsecsign','2');
				this.setSelectData('typeList','parins',this.formData.parins);
			})
			.catch(err=>{})
		},
		//获取小表单拥有字段
		getFormKey(){
			let formKeys = this.$util.getFormKeys(this.minFormName,this.regnoInfo.enttype);
			this.formKeys = formKeys;
			let keys = Object.keys(formKeys);
			keys.forEach(item=>{
				this.rules[item] = this.rulesAll[item];
			})
		},
		submit(){
			let _this = this;
			this.$refs.uForm.validate().then(res => {
				this.saveBtn();
			}).catch(errors => {
				this.$util.showToast({title:'请完善信息'})
			})
		},
		saveBtn(){
			let data = {
				regno: this.regnoInfo.regno,
				ancheyear: this.year,
			}
			if(this.ancheid){
				data.ancheid = this.ancheid;
			}
			data = this.$util.setSaveFormData(this.formKeys,data,this.formData);
			this.$http.sendRequest({
				url: '/annual/report/baseinfo/update-partybuilding',
				method: 'PUT',
				data: data
			})
			.then(res=>{
				this.setRecord();
			})
			.catch(err=>{})
		},
		setInit(objWrapName,dataName,initData){
			let con = this[objWrapName][dataName];
			if(con===''||con===null||con===undefined){
				this[objWrapName][dataName] = initData
			}
		},
		selectBtn(e){
			this.setSelectData('typeList','parins',e.id);
			this.$refs.uForm.validateField('parins');
		},
		setSelectData(listName,dataName,id){
			let arr = this[listName];
			let obj = {};
			for(let i=0; i<arr.length; i++){
				if(arr[i].id == id){
					obj = arr[i];
					break;
				}
			}
			if(obj.id){
				this.formData[dataName] = obj.id;
				this.formData[`${dataName}Cn`] = obj.name;
				console.log(55555,this.formData[dataName],this.formData[`${dataName}Cn`])
			}
		},
		
		
		
		
		
		//存一下记录
		setRecord(){
			let fillingItem = this.$minFormType.get(this.minFormName);
			this.$http.sendRequest({
				url: '/annual/report/record',
				method: 'POST',
				data: {
					regno: this.regnoInfo.regno,
					year: this.year,
					fillingItem: fillingItem,
					isImp: '0',
				},
				hideLoad: true,
			})
			.then(res=>{
				uni.navigateBack();
			})
			.catch(err=>{})
		},
		goPage(path){
			uni.navigateTo({
				url: path
			})
		},
	}
}
</script>

<style lang="less" scoped>
.wrap{
	width: 100vw;
	min-height: 100vh;
	padding-top: 16rpx;
	box-sizing: border-box;
	position: relative;
	background-color: #FFF;
	
	.topBc{
		height: 10rpx;
		background-color: #F5F6F8;
	}
}

.content{
	padding: 30rpx 36rpx;
	background-color: #FFF;
	
	.title{
		color: #E4B269;
		font-size: 36rpx;
		font-weight: bold;
		border-left: 8rpx solid #E4B269;
		line-height: 40rpx;
		padding-left: 16rpx;
		margin-bottom: 30rpx;
		margin-top: 30rpx;
	}
	
	
}
</style>